﻿Imports System.IO

Public Class Main
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ProgressBar.Visible = False
    End Sub

    ' Liste des MTZ Disponibles
    Private Sub ListeMtz_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListeMtz.Click
        Dim Listing
        Listing = New ClassListe("MTZ", ListeMtz)
    End Sub

    ' Liste des Thèmes Disponibles
    Private Sub ListeThemes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListeThemes.Click
        Dim Listing
        Listing = New ClassListe("Folder", ListeThemes)
    End Sub

    ' Extraction du MTZ Selectionné
    Private Sub BtnExtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExtract.Click
        ProgressBar.Visible = True
        Dim MtzSelected = ListeMtz.Text
        Dim basePath = My.Application.Info.DirectoryPath
        Dim mtzDir = basePath & "\1--------MTZ\"
        Dim tempDir = basePath & "\4---Temp\"
        Dim outputDir = basePath & "\3---Sortie\"
        ProgressBar.Value = 10

        Dim CPY = CreateObject("Scripting.FileSystemObject")
        CPY.CopyFile(mtzDir & MtzSelected, tempDir & MtzSelected)
        ProgressBar.Value = ProgressBar.Value + 2

        ' Extraction du mtz principal
        Dim ExtractMtz
        ExtractMtz = New ClassExtract7z(tempDir & MtzSelected, tempDir)

        ' Attente que le process soit fini
        Dim Wait
        Wait = New ClassWaitProcess("7zip")

        ' Suppression du mtz de temp
        Kill(tempDir & MtzSelected)
        ProgressBar.Value = ProgressBar.Value + 5

        ' Creation du dossier de sortie
        Dim DirNameMtz = MtzSelected.Remove(MtzSelected.Length - 4)
        My.Computer.FileSystem.CreateDirectory(outputDir & DirNameMtz)

        ' Copie des folders et du xml
        If File.Exists(tempDir & "description.xml") Then
            File.Move(tempDir & "description.xml", outputDir & DirNameMtz & "\description.xml")
        End If
        If Directory.Exists(tempDir & "wallpaper") Then
            Directory.Move(tempDir & "wallpaper", outputDir & DirNameMtz & "\wallpaper")
        End If
        If Directory.Exists(tempDir & "preview") Then
            Directory.Move(tempDir & "preview", outputDir & DirNameMtz & "\preview")
        End If
        If Directory.Exists(tempDir & "ringtones") Then
            Directory.Move(tempDir & "ringtones", outputDir & DirNameMtz & "\ringtones")
        End If
        If Directory.Exists(tempDir & "audioeffect") Then
            Directory.Move(tempDir & "audioeffect", outputDir & DirNameMtz & "\audioeffect")
        End If
        ProgressBar.Value = ProgressBar.Value + 1

        Dim di As New IO.DirectoryInfo(tempDir.Remove(tempDir.Length - 1))
        Dim extFi As IO.FileInfo() = di.GetFiles("*") 'filtrer les fichiers
        Dim FileApp As IO.FileInfo


        For Each FileApp In extFi
            Dim appZip = FileApp.Name & ".zip"
            Rename(tempDir & FileApp.Name, tempDir & appZip)
            My.Computer.FileSystem.CreateDirectory(tempDir & FileApp.Name)
            Dim ExtractApp
            ExtractApp = New ClassExtract7z(tempDir & appZip, tempDir & FileApp.Name)
            Wait = New ClassWaitProcess("7zip")
            Kill(tempDir & appZip)
            Directory.Move(tempDir & FileApp.Name, outputDir & DirNameMtz & "\" & FileApp.Name)
            ProgressBar.Value = ProgressBar.Value + 1
        Next

        ProgressBar.Value = 100
        MsgBox(MtzSelected & " extraction OK")
        ProgressBar.Visible = False
    End Sub

    ' Reconstruction du Thème sélectionné
    Private Sub BtnConstruire_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConstruire.Click
        ProgressBar.Visible = True
        ProgressBar.Value = 1
        Dim ThemeSelected = ListeThemes.Text
        Dim basePath = My.Application.Info.DirectoryPath
        Dim themesDir = basePath & "\2--------Themes\"
        Dim tempDir = basePath & "\4---Temp\"
        Dim outputDir = basePath & "\3---Sortie\"
        Dim Wait
        ProgressBar.Value = ProgressBar.Value + 1
        ' Copie des folders et du xml
        If File.Exists(themesDir & ThemeSelected & "\description.xml") Then
            File.Move(themesDir & ThemeSelected & "\description.xml", tempDir & "description.xml")
        End If
        If Directory.Exists(themesDir & ThemeSelected & "\wallpaper") Then
            Directory.Move(themesDir & ThemeSelected & "\wallpaper", tempDir & "wallpaper")
        End If
        If Directory.Exists(themesDir & ThemeSelected & "\preview") Then
            Directory.Move(themesDir & ThemeSelected & "\preview", tempDir & "preview")
        End If
        If Directory.Exists(themesDir & ThemeSelected & "\ringtones") Then
            Directory.Move(themesDir & ThemeSelected & "\ringtones", tempDir & "ringtones")
        End If
        If Directory.Exists(themesDir & ThemeSelected & "\audioeffect") Then
            Directory.Move(themesDir & ThemeSelected & "\audioeffect", tempDir & "audioeffect")
        End If
        ProgressBar.Value = ProgressBar.Value + 10
        'Zip des applications
        Dim di As New IO.DirectoryInfo(themesDir & ThemeSelected)
        Dim extFi As IO.DirectoryInfo() = di.GetDirectories("*") 'filtrer les fichiers
        Dim FolderApp As IO.DirectoryInfo


        For Each FolderApp In extFi
            Dim appZip = FolderApp.Name & ".zip"
            Dim ZippingApp
            ZippingApp = New ClassZip7z(themesDir & ThemeSelected & "\" & FolderApp.Name, tempDir & appZip)
            Wait = New ClassWaitProcess("7zip")
            Rename(tempDir & appZip, tempDir & FolderApp.Name)
            ProgressBar.Value = ProgressBar.Value + 1
        Next

        ' Creation du mtz
        ProgressBar.Value = 80
        Dim BuildMtz
        BuildMtz = New ClassZip7z(tempDir, outputDir & ThemeSelected & ".mtz")
        Wait = New ClassWaitProcess("7zip")
        ProgressBar.Value = 100


        ' Copie des folders et du xml
        If File.Exists(tempDir & "description.xml") Then
            File.Move(tempDir & "description.xml", themesDir & ThemeSelected & "\description.xml")
        End If
        If Directory.Exists(tempDir & "wallpaper") Then
            Directory.Move(tempDir & "wallpaper", themesDir & ThemeSelected & "\wallpaper")
        End If
        If Directory.Exists(tempDir & "preview") Then
            Directory.Move(tempDir & "preview", themesDir & ThemeSelected & "\preview")
        End If
        If Directory.Exists(tempDir & "ringtones") Then
            Directory.Move(tempDir & "ringtones", themesDir & ThemeSelected & "\ringtones")
        End If
        If Directory.Exists(tempDir & "audioeffect") Then
            Directory.Move(tempDir & "audioeffect", themesDir & ThemeSelected & "\audioeffect")
        End If
        


        MsgBox(ThemeSelected & " reconstruit avec succès")
        Kill(tempDir & "*")

        Dim objFSO = CreateObject("Scripting.FileSystemObject")
        objFSO.DeleteFolder(tempDir.Remove(tempDir.Length - 1))
        My.Computer.FileSystem.CreateDirectory(tempDir.Remove(tempDir.Length - 1))
        ProgressBar.Visible = False
    End Sub




End Class
